/***
 * @Author: 玄绮 shijiahansi@qq.com
 * @Date: 2024-05-22 19:41:01
 * @LastEditors: 玄绮 shijiahansi@qq.com
 * @LastEditTime: 2024-05-24 12:47:14
 * @FilePath: \client\src\app\client\Client.h
 * @Description:
 * @
 * @Copyright (c) 2024 by 玄绮, All Rights Reserved.
 */

#ifndef __CLIENT_H__
#define __CLIENT_H__

#include <ChatClient.h>
#include <QObject>
#include <QTcpSocket>
#include <QThread>
#include <data/File.pb.h>
#include <data/Message.pb.h>
#include <data/UserInfo.pb.h>
#include <iostream>
#include <iterator>
#include <qobject.h>
#include <qobjectdefs.h>
#include <thread>

using namespace proto;

class Client : public ChatClient {
    Q_OBJECT
    QTcpSocket* socket;

protected:
    UserInfo                                   myself;
    std::vector<std::shared_ptr<FriendInfo>>   m_firends;
    std::vector<std::shared_ptr<ChatRoomInfo>> m_rooms;

public:
    Client();
    ~Client();

    void connectToHost(QString address, int port);

    std::vector<std::shared_ptr<FriendInfo>>   firends();
    std::vector<std::shared_ptr<ChatRoomInfo>> rooms();

protected:
    void onReadyRead();

protected:
    virtual void send_packge(::proto::packge::Packge::shared_ptr pack) override;

    virtual void onRegister(int code, String message, std::shared_ptr<UserInfo> user) override;
    virtual void onLogin(int code, String message, std::shared_ptr<UserInfo> user) override;
    virtual void onGetUserInfo(int code, String message, std::shared_ptr<UserInfo> user) override;
    virtual void onChangUserInfo(int code, String message, std::shared_ptr<UserInfo> user) override;
    virtual void onSearchAuthor(int code, String message, std::vector<std::shared_ptr<UserInfo>> users) override;
    virtual void onSearchGroup(int code, String message, std::vector<std::shared_ptr<ChatRoomInfo>> rooms) override;
    virtual void onAddFriends(int code, String message, std::shared_ptr<FriendInfo> info) override;
    virtual void onDelFriends(int code, String message, String firendsId) override;
    virtual void onCreateGroup(int code, String message, std::shared_ptr<ChatRoomInfo> room) override;
    virtual void onCreateRoom(int code, String message, std::shared_ptr<ChatRoomInfo> room) override;
    virtual void onJoinGroup(int code, String message, std::shared_ptr<ChatRoomInfo> room) override;
    virtual void onQuitGroup(int code, String message, String roomId) override;
    virtual void onSetAccount(int code, String message) override;
    virtual void onSetUserInfo(int code, String message, std::shared_ptr<UserInfo> user) override;
    virtual void onGetFriendList(int code, String message, std::vector<std::shared_ptr<FriendInfo>> users) override;
    virtual void onGetRoomList(int code, String message, std::vector<std::shared_ptr<ChatRoomInfo>> rooms) override;
    virtual void onGetRoomMembers(int code, String message, String roomId, std::vector<std::shared_ptr<UserInfo>>) override;
    virtual void onVerifyVerificationCode(int code, String message) override;
    virtual void onSendVerificationCode(int code, String message, String timeout) override;

    virtual void onMessage(std::shared_ptr<proto::data::Message> msg) override;

signals:

    void signalConnected();
    void signalDiconnected();
    void signalConnectError();

signals:
    void signalRegister(int code, QString message, std::shared_ptr<UserInfo> info);
    void signalLogin(int code, QString message);

    void signalSearchAuthor(std::vector<std::shared_ptr<UserInfo>> users);
    void signalSearchGroup(std::vector<std::shared_ptr<ChatRoomInfo>> rooms);
    void signalAddFriends(int code, QString message, std::shared_ptr<FriendInfo> info);
    void signalDelFriends(int code, QString message, String firendsId);
    void signalCreateGroup(int code, QString message, std::shared_ptr<ChatRoomInfo> room);
    void signalCreateRoom(int code, QString message, std::shared_ptr<ChatRoomInfo> room);
    void signalJoinGroup(int code, QString message, std::shared_ptr<ChatRoomInfo> room);
    void signalQuitGroup(int code, QString message, String roomId);
    void signalSetAccount(int code, QString message);

    void signalGetFriendList(int code = 0, QString message = "");
    void signalGetRoomList(int code = 0, QString message = "");
    void signalMessage(std::shared_ptr<data::Message> message);
    void signalSetUserInfo(int code, QString message);
    void signalVerifyVerificationCode(int code, QString message);
    void signalSendVerificationCode(int code, QString message, QString timeout);
    // void signalGetFileOver(QString url);
};

#endif  // __CLIENT_H__